package com.example.spingbootvue.mapper;


import com.example.spingbootvue.entity.Resumemsg;
import com.example.spingbootvue.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    //注册表信息插入数据库
    @Insert("INSERT INTO user ( account, password, position, email) VALUES ( #{account}, #{password}, #{position}, #{email})")
    int insertAll(String account,String password,String position,String email);

    //查询用户信息
    @Select("select * from user ")
    List<User> selectAll();

    //JWT功能实现，根据token中的header解码后的id查询用户信息
    @Select("SELECT * from user WHERE id=#{id}")
    User selectById(Integer id);

    //登录界面查询用户输入的账户和密码是否在数据中存在
    @Select("select account,password,isban,position,role,id,image,SickName from user where account=#{account} and password=#{password} limit 1")
    User selectnameandpassword(String account, String password);
    //查询不到数据返回空值

    //user管理界面用户点击修改按钮可以修改注册信息
    @Update("UPDATE user SET  account= #{account}, password= #{password}, position= #{position}, email= #{email} WHERE id= #{id}")
    int updateAll(String account,String password,String position,String email,Integer id);

    //user管理界面点击删除按钮能够将用户的信息删除
    @Delete("Delete from user where id=#{id}")
    int delete(Integer id);

    //判断注册页面重复账号不能注册
    @Select("select * from user where account=#{account} or email=#{email} limit 1 ")
    User findbyaccount(String account,String email);

    //将用户降权为普通用户
    //这里从前端传入了一个值id, 没有传入role,mapper层可以直接和数据库简历连接,进行更新操作.
    @Update("UPDATE user SET role = 'user' WHERE id=#{id}")
    int relimit(Integer id);

    //将普通用户升级为管理员
    @Update("UPDATE user SET role = 'admin' WHERE id=#{id}")
    int uplimit(Integer id);

    //将用户封禁,用户无法登录,更新isban
    @Update("UPDATE user SET isban = 0 WHERE id=#{id}")
    int banaccount(Integer id);

    //将用户解除封禁
    @Update("UPDATE user SET isban = 1 WHERE id=#{id}")
    int removeaccount(Integer id);

    //插入前端上传的图片地址码
    @Update("UPDATE  user_biographical_notes SET image= #{image} where account = #{account}")
    int insertcode(String image,String account);

    //查询用户个人简历上传的图片
    @Select("SELECT image from user_biographical_notes where account =#{account}")
    Resumemsg selectcode(String account);

    //删除个人简介的图片
    @Update("UPDATE user_biographical_notes SET image=0 where account=#{account}")
    int deletecode(String account);

    //登录时随机生成图片作为社区头像,数据库更新数据
    @Update("UPDATE user SET image = #{image} WHERE account = #{account}")
    int updateimage(String account,String image);

    //将昵称和随机图片等信息查询出来
    @Select("SELECT * FROM user WHERE account = #{account}")
    User selectByaccount(String account);

    //随机生成图片已存在则不需要更新
    @Select("select * from user where account=#{account} AND image IS NOT NULL limit 1 ")
    User queryImage(String account,String image);

    //登录时随机生成昵称作为社区昵称,数据库更新数据
    @Update("UPDATE user SET SickName = #{sickname} WHERE account = #{account}")
    int updateSickName(String account,String sickname);

    //随机生成昵称已存在则不需要更新
    @Select("select * from user where account=#{account} AND sickname IS NOT NULL limit 1 ")
    User querySickName(String account,String sickname);
}
